package com.css.apps.base.user.common;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.common.provider.json.OrgJsonProvider;
import com.css.apps.base.user.model.SUser;
import com.css.db.page.Page;
import com.css.db.query.QueryCache;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.UserAction;
import com.opensymphony.xwork.Action;

@SuppressWarnings("serial")
public class SelectUserList extends UserAction {
	
	private static Log log = LogFactory.getLog(SelectUserList.class);
	private String realName = null;
	private String funId = null;
	private Page page;

	public SelectUserList() {
		page = new Page();
		page.setCountField("a.uuid");
	}

	public String userGo() {
		try {
			QueryCache qc = new QueryCache("select a.uuid from SUser a " + getWhere() + getOrder());
			setWhere(qc);
			page = qc.page(page);
			page.setResults(QueryCache.idToObj(SUser.class, page.getResults()));
			OrgJsonProvider json = new OrgJsonProvider();
			result = json.getAllOrgUserTree("8a8d81bc44fc43890144fc438d240000").toString();
			return Action.SUCCESS;
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			setMessage(Messages.getString("systemMsg.exception"));
			return Action.ERROR;
		}
	}

	public String getWhere() {
		StringBuffer sb = new StringBuffer(" where 1=1 ");
		if (StringHelper.isNotEmpty(realName))
			sb.append("and a.realName like :realName ");
		return sb.toString();
	}

	public void setWhere(QueryCache qc) {
		if (StringHelper.isNotEmpty(realName))
			qc.setParameter("realName", "%" + realName + "%");
	}

	public String getOrder() {
		return StringHelper.isNotEmpty(page.getOrderByString()) ? page.getOrderByString() : " order by a.realName";
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public String getFunId() {
		return funId;
	}

	public void setFunId(String funId) {
		this.funId = funId;
	}
}
